/**
 * Created by ss on 2015/1/13.
 */
(function ($) {
    var ui = {
        //导航current
        $nav: $('.nav'),
        $now_url: window.location.href,
        //搜索空判断
        $search: $('.search_form'),
        $search_input: $('.search_input'),
        $search_label: $('.search_label'),
        //dialog
        //通用
        $dialog: $('.ui_dialog'),
        $document_width: $(window).width() / 2,
        $document_height: $(window).height() / 2,
        $dialog_width: function () {
            return this.$dialog.width() / 2;
        },
        $dialog_height: function () {
            return this.$dialog.height() / 2;
        },
        $dialog_close: $('.ui_dialog_title_close,.ui_dialog_title_close_xqtj'),
        $dialog_bg: $('.ui_dialog_bg'),
        $old_account: $('.old_account'),
        //注册
        $reg_btn: $('.reg_btn'),
        $reg: $('.reg_dialog'),
        $reg_form: $('form.reg_form'),
        $username_reg: $('#username_reg'),
        $email_reg: $('#email_reg'),
        $password_reg: $('#password_reg'),
        //登录
        $log_btn: $('.log_btn'),
        $log: $('.log_dialog'),
        $log_form: $('form.log_form'),
        $user_log: $('#user_log'),
        $password_log: $('#password_log'),
        //忘记密码
        $forget_box: $('.forget_dialog'),
        $forget_btn: $('.forget_btn'),
        //需求提交
        $ui_dialog_xqtj: $('.ui_dialog_xqtj'),
        $dialog_width_xqtj: function () {
            return this.$ui_dialog_xqtj.width() / 2;
        },
        $dialog_height_xqtj: function () {
            return this.$ui_dialog_xqtj.height() / 2;
        },
        $xqtj_btn: $('.xqtj_btn'),
        $xqtj: $('.xqtj_dialog'),
        $xqtj_form: $('.xqtj_form'),
        $xqbt: $('#xqbt'),
        $xqnr: $('#xqnr'),
        $back_account: $('.back_account'),
        $back_log:$('.back_log')
    };

    var oPage = {
        //注册事件
        init: function () {
            this.nav_current();
            this.search_null();
            this.dialog();
            this.close_dialog();
            this.open_dialog_reg();
            this.open_dialog_log();
            this.open_forget_password();
            this.open_account();
            this.reg_form();
            this.log_form();
            this.open_dialog_xqtj();
            this.xqtj_form();
            this.back_account();
            this.back_log();
        },
        //导航current
        nav_current: function () {
            ui.$nav.find('a').each(function () {
                var $nav_href = $(this).attr('href');
                var local_attr = $nav_href.substr($nav_href.lastIndexOf('/') + 1);
                //正则删除包括问号以后的所有代码
                var href_attr = ui.$now_url.substr(ui.$now_url.lastIndexOf('/') + 1).replace(/\?.+/, "");
                if (local_attr.toLocaleLowerCase() == href_attr.toLocaleLowerCase()) {
                    $(this).attr('class', 'current');
                }
            });
        },
        //搜索空判断
        search_null: function () {
            ui.$search.on('submit', function (e) {
                if (ui.$search_input.val() === '') {
                    ui.$search_label.show();
                }
                e.stopPropagation();
            });
            ui.$search_input.on('focus', function () {
                ui.$search_label.hide();
            });
        },
        //打开注册dialog
        open_dialog_reg: function () {
            ui.$reg_btn.on('click', function () {
                ui.$dialog_bg.fadeIn();
                ui.$reg.fadeIn();
            });
        },
        open_account: function () {
            ui.$old_account.on('click', function () {
                ui.$reg.fadeOut();
                ui.$log.fadeIn();
            });
        },
        //打开登录dialog
        open_dialog_log: function () {
            ui.$log_btn.on('click', function () {
                ui.$dialog_bg.fadeIn();
                ui.$log.fadeIn();
            });
        },
        back_account: function () {
            ui.$back_account.on('click', function () {
                ui.$log.fadeOut();
                ui.$reg.fadeIn();
            });
        },
        //忘记密码dialog
        open_forget_password: function () {
            ui.$forget_btn.on('click', function () {
                ui.$log.hide();
                ui.$forget_box.show();
            });
        },
        back_log:function(){
            ui.$back_log.on('click', function () {
                ui.$forget_box.hide();
                ui.$log.show();
            });
        },
        //打开需求提交dialog
        open_dialog_xqtj: function () {
            ui.$xqtj_btn.on('click', function () {
                ui.$dialog_bg.fadeIn();
                ui.$xqtj.fadeIn();
            });
        },
        //关闭dialog
        close_dialog: function () {
            var close_fun = function () {
                ui.$dialog_bg.fadeOut();
                ui.$dialog.fadeOut();
                ui.$ui_dialog_xqtj.fadeOut();
                //隐藏错误提示
                ui.$reg_form.find('input').next().hide();
                ui.$log_form.find('input').next().hide();
                ui.$xqtj_form.find('input').next().hide();
            };
            ui.$dialog_close.on('click', function () {
                close_fun();
            });
            $(window).keydown(function (event) {
                if (event && event.keyCode == 27) {
                    close_fun();
                }
            });
        },
        //dialog定位
        dialog: function () {
            $('.ui_dialog').css({
                'left': ui.$document_width - ui.$dialog_width() + 'px',
                'top': ui.$document_height - ui.$dialog_height() - 80 + 'px'
            });
            $('.ui_dialog_xqtj').css({
                'left': ui.$document_width - ui.$dialog_width_xqtj() + 'px',
                'top': ui.$document_height - ui.$dialog_height_xqtj() - 10 + 'px'
            });
        },
        //注册表单验证
        reg_form: function () {
            ui.$reg_form.on('submit', function (e) {
                if (ui.$username_reg.val() === '') {
                    ui.$username_reg.next().show();
                }
                if (ui.$email_reg.val() === '') {
                    ui.$email_reg.next().show();
                }
                if (ui.$password_reg.val() === '') {
                    ui.$password_reg.next().show();
                }
                e.stopPropagation();
            });
            ui.$username_reg.on('focus', function () {
                ui.$username_reg.next().hide();
            });
            ui.$email_reg.on('focus', function () {
                ui.$email_reg.next().hide();
            });
            ui.$password_reg.on('focus', function () {
                ui.$password_reg.next().hide();
            });
        },
        //登录表单验证
        log_form: function () {
            ui.$log_form.on('submit', function (e) {
                if (ui.$user_log.val() === '') {
                    ui.$user_log.next().show();
                }
                if (ui.$password_log.val() === '') {
                    ui.$password_log.next().show();
                }
                e.stopPropagation();
            });
            ui.$user_log.on('focus', function () {
                ui.$user_log.next().hide();
            });
            ui.$password_log.on('focus', function () {
                ui.$password_log.next().hide();
            });
        },
        xqtj_form: function () {
            ui.$xqtj_form.on('submit', function (e) {
                if (ui.$xqbt.val() === '') {
                    alert('标题不能为空');
                    return false;
                }
                if (ui.$xqnr.val() === '') {
                    alert('内容不能为空');
                    return false;
                }
                e.stopPropagation();
            });
        }
    };
    oPage.init();
})(jQuery);